import { get, post, put, del } from '@/utils/request';

/**
 * 统计数据API服务
 */

// ==================== 用户统计 ====================

/**
 * 获取用户统计数据
 */
export async function getUserStatistics(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<STATISTICS.UserStatistics>> {
  return get('/admin/statistics/users', params);
}

/**
 * 获取用户增长趋势
 */
export async function getUserGrowthTrend(params: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<STATISTICS.ChartDataPoint[]>> {
  return get('/admin/statistics/users/growth-trend', params);
}

/**
 * 获取用户活跃度统计
 */
export async function getUserActivityStats(params: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/users/activity', params);
}

/**
 * 获取用户分布统计
 */
export async function getUserDistributionStats(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/users/distribution', params);
}

// ==================== 学习统计 ====================

/**
 * 获取课程统计数据
 */
export async function getCourseStatistics(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/courses', params);
}

/**
 * 获取学习统计数据
 */
export async function getLearningStatistics(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<STATISTICS.LearningStatistics>> {
  return get('/admin/statistics/learning', params);
}

/**
 * 获取学习时长趋势
 */
export async function getStudyTimeTrend(params: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<STATISTICS.ChartDataPoint[]>> {
  return get('/admin/statistics/learning/study-time-trend', params);
}

/**
 * 获取练习统计
 */
export async function getPracticeStatistics(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/learning/practice', params);
}

/**
 * 获取课程完成率统计
 */
export async function getCourseCompletionStats(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/learning/course-completion', params);
}

/**
 * 获取学习模式分析
 */
export async function getLearningPatternAnalysis(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/learning/patterns', params);
}

// ==================== 内容统计 ====================

/**
 * 获取内容统计数据
 */
export async function getContentStatistics(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<STATISTICS.ContentStatistics>> {
  return get('/admin/statistics/content', params);
}

/**
 * 获取内容参与度统计
 */
export async function getContentEngagementStats(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/content/engagement', params);
}

/**
 * 获取热门内容排行
 */
export async function getPopularContentRanking(params?: { type?: string; limit?: number } & STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/content/popular', params);
}

/**
 * 获取内容质量分析
 */
export async function getContentQualityAnalysis(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/content/quality', params);
}

// ==================== 系统统计 ====================

/**
 * 获取系统统计数据
 */
export async function getSystemStatistics(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<STATISTICS.SystemStatistics>> {
  return get('/admin/statistics/system', params);
}

/**
 * 获取系统性能指标
 */
export async function getSystemPerformanceMetrics(params: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/system/performance', params);
}

/**
 * 获取API使用统计
 */
export async function getApiUsageStatistics(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/system/api-usage', params);
}

/**
 * 获取错误统计
 */
export async function getErrorStatistics(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/system/errors', params);
}

/**
 * 获取用户会话统计
 */
export async function getUserSessionStatistics(params?: STATISTICS.StatisticsQueryParams): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/system/user-sessions', params);
}

/**
 * 获取存储使用统计
 */
export async function getStorageUsageStatistics(): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/system/storage-usage');
}

// ==================== 实时统计 ====================

/**
 * 获取实时统计数据
 */
export async function getRealTimeStatistics(): Promise<STATISTICS.ApiResponse<STATISTICS.RealTimeStatistics>> {
  return get('/admin/statistics/realtime');
}

/**
 * 获取实时在线用户
 */
export async function getRealTimeOnlineUsers(): Promise<STATISTICS.ApiResponse<{ count: number; users: any[] }>> {
  return get('/admin/statistics/realtime/online-users');
}

/**
 * 获取实时活动流
 */
export async function getRealTimeActivityStream(limit?: number): Promise<STATISTICS.ApiResponse<any[]>> {
  return get('/admin/statistics/realtime/activity-stream', { limit });
}

/**
 * 获取实时系统负载
 */
export async function getRealTimeSystemLoad(): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/realtime/system-load');
}

// ==================== 数据导出 ====================

/**
 * 导出统计数据
 */
export async function exportStatistics(params: STATISTICS.ExportParams): Promise<Blob> {
  return get('/admin/statistics/export', params, { responseType: 'blob' });
}

/**
 * 生成统计报告
 */
export async function generateStatisticsReport(params: {
  report_type: 'daily' | 'weekly' | 'monthly' | 'quarterly' | 'yearly';
  data_types: string[];
  start_date?: string;
  end_date?: string;
  format: 'pdf' | 'excel';
  include_charts?: boolean;
}): Promise<Blob> {
  return post('/admin/statistics/generate-report', params, { responseType: 'blob' });
}

/**
 * 获取报告生成状态
 */
export async function getReportGenerationStatus(taskId: string): Promise<STATISTICS.ApiResponse<{ status: string; progress: number; download_url?: string }>> {
  return get(`/admin/statistics/report-status/${taskId}`);
}

// ==================== 自定义统计 ====================

/**
 * 创建自定义统计查询
 */
export async function createCustomStatisticsQuery(params: {
  name: string;
  description?: string;
  query_config: any;
  chart_config?: any;
}): Promise<STATISTICS.ApiResponse<any>> {
  return post('/admin/statistics/custom-queries', params);
}

/**
 * 执行自定义统计查询
 */
export async function executeCustomStatisticsQuery(queryId: number, params?: any): Promise<STATISTICS.ApiResponse<any>> {
  return post(`/admin/statistics/custom-queries/${queryId}/execute`, params);
}

/**
 * 获取自定义统计查询列表
 */
export async function getCustomStatisticsQueries(): Promise<STATISTICS.ApiResponse<any[]>> {
  return get('/admin/statistics/custom-queries');
}

/**
 * 删除自定义统计查询
 */
export async function deleteCustomStatisticsQuery(queryId: number): Promise<STATISTICS.ApiResponse<null>> {
  return del(`/admin/statistics/custom-queries/${queryId}`);
}

// ==================== 统计配置 ====================

/**
 * 获取统计配置
 */
export async function getStatisticsConfig(): Promise<STATISTICS.ApiResponse<any>> {
  return get('/admin/statistics/config');
}

/**
 * 更新统计配置
 */
export async function updateStatisticsConfig(config: any): Promise<STATISTICS.ApiResponse<any>> {
  return put('/admin/statistics/config', config);
}

/**
 * 刷新统计缓存
 */
export async function refreshStatisticsCache(type?: string): Promise<STATISTICS.ApiResponse<null>> {
  return post('/admin/statistics/refresh-cache', { type });
}

/**
 * 导出统计数据
 */
export async function exportStatisticsData(params: {
  type: string;
  format: 'excel' | 'csv' | 'pdf';
  start_date?: string;
  end_date?: string;
}): Promise<any> {
  return post('/admin/statistics/export', params, { responseType: 'blob' });
}
